package UI;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JTextField;
import javax.swing.JButton;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;

import net.webservicex.stockcurrencyconvert.StockPriceConvert;

public class stockconverterUI extends JFrame {

	private JPanel contentPane;
	private JTextField symbolText;
	private StockPriceConvert spc;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					stockconverterUI frame = new stockconverterUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws MalformedURLException 
	 */
	public stockconverterUI() throws MalformedURLException {
		spc = connectToWebService();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 541, 301);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.NORTH);
		
		JLabel lblStockCurrencyConverter = new JLabel("Stock Currency Converter");
		panel_1.add(lblStockCurrencyConverter);
		
		JPanel panel = new JPanel();
		contentPane.add(panel, BorderLayout.WEST);
		panel.setLayout(new GridLayout(6, 2, 0, 0));
		
		
		JPanel panel_2 = new JPanel();
		contentPane.add(panel_2, BorderLayout.CENTER);
		panel_2.setLayout(null);
		
		final JLabel companyName = new JLabel("");
		companyName.setBounds(23, 21, 310, 14);
		panel_2.add(companyName);
		
		final JLabel conversion = new JLabel("");
		conversion.setBounds(23, 68, 310, 14);
		panel_2.add(conversion);
		
		JLabel symbolLabel = new JLabel("Stock Symbol");
		panel.add(symbolLabel);
		
		symbolText = new JTextField();
		panel.add(symbolText);
		symbolText.setColumns(10);
		
		JLabel currencyLabel = new JLabel("Currency");
		panel.add(currencyLabel);
		
		final JComboBox<String> currencyCombo = new JComboBox<String>() ;
		currencyCombo.addItem("USD - $"); // usd
		currencyCombo.addItem("EUR - €");// euro
		currencyCombo.addItem("GBP - £");// brittish pound
		currencyCombo.addItem("CAD - $");// canadian dollar
		currencyCombo.addItem("AUD - $");// australian dollar
		currencyCombo.addItem("MXN - $");// mexican peso
		currencyCombo.addItem("HKD - $");// hong kong dollar
		currencyCombo.addItem("JPY - ¥");// japanese yen
		currencyCombo.addItem("CNY - ¥");// rmb
		currencyCombo.addItem("CHF - Fr");// swiss franc
		currencyCombo.addItem("KRW - ₩");// korean won
		currencyCombo.addItem("ZAR - R");// south african ran
		currencyCombo.addItem("SEK - Kr");// swedish krone
		currencyCombo.addItem("NOK - Kr");// norwegian krone
		currencyCombo.addItem("BRL - R$");// brazilian real
		currencyCombo.addItem("RUB - ₱");// russian rouble
		
		
		panel.add(currencyCombo);
		
		JLabel quoteLabel = new JLabel("Quote Info");
		panel.add(quoteLabel);
		
		final JComboBox <String> QuoteInfoCombo = new JComboBox <String>();
		QuoteInfoCombo.addItem("Open");
		QuoteInfoCombo.addItem("Close");
		QuoteInfoCombo.addItem("Daily High");
		QuoteInfoCombo.addItem("Daily Low");
		QuoteInfoCombo.addItem("Annual Range");
		QuoteInfoCombo.addItem("Change");
		
		panel.add(QuoteInfoCombo);
		
		JButton convertButton = new JButton("Convert");
		
		convertButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				String currency = ((String) currencyCombo.getSelectedItem()).split(" ")[0].trim();
				String quoteInfo = (String) QuoteInfoCombo.getSelectedItem();
				String symbol = symbolText.getText();
				String s="";
				try{
					if (quoteInfo.equalsIgnoreCase("open")){
						s = spc.convertStockOpen(symbol, currency);
					}
					else if (quoteInfo.equalsIgnoreCase("close")){
						s = spc.convertStockClose(symbol, currency);
					}
					else if (quoteInfo.equalsIgnoreCase("daily high")){
						s = spc.convertStockHigh(symbol, currency);
					}
					else if (quoteInfo.equalsIgnoreCase("daily low")){
						s = spc.convertStockLow(symbol, currency);
					}
					else if (quoteInfo.equalsIgnoreCase("annual range")){
						s = spc.convertAnnualRange(symbol, currency);
					}
					else if (quoteInfo.equalsIgnoreCase("change")){
						s = spc.convertStockChange(symbol, currency);
					}
					companyName.setText("Company name:\n "+ spc.getName(symbol));
				}
				catch(Exception exception){
					companyName.setText("");
					s="Invalid Stock Symbol";
				}
				conversion.setText(s);
				
			}
		});
		panel.add(convertButton);
		
	}
	private StockPriceConvert connectToWebService() throws MalformedURLException{
		URL url = new URL(String.format("http://localhost:8085/ts?wsdl", 8085));
		
		QName qname = new QName("http://stockcurrencyconvert.webservicex.net/","StockPriceConvertImpService");
		 Service service = Service.create(url, qname);
		 StockPriceConvert eif = service.getPort(StockPriceConvert.class);
		 return eif;
	}
}
